fix: guard checkDiffFile against mode-only diffs with empty OrigName/NewName#221
Open
ktersius wants to merge 2 commits into
Open
fix: guard checkDiffFile against mode-only diffs with empty OrigName/NewName#221ktersius wants to merge 2 commits into
ktersius wants to merge 2 commits into
Conversation
…NewName
Mode-only diffs (e.g. chmod changes) and some binary diffs produce no
--- / +++ file headers in the unified diff output. The sourcegraph/go-diff
parser therefore returns empty OrigName and NewName strings for those
FileDiff entries. checkDiffFile assumed at least one '/' was always present
(from the 'a/' or 'b/' prefix) and unconditionally accessed index [1] after
SplitN, causing a runtime panic:
panic: runtime error: index out of range [1] with length 1
diff/diff.go:44
Guard the slice length before indexing and skip entries with no file headers
— there is no content to scan in mode-only or header-less diffs.
Add a table entry to Test_checkDiffFile covering the case where OrigName
and NewName are both empty strings — the shape go-diff produces for
mode-only diffs (chmod changes with no content diff).
Before the fix, calling checkDiffFile with empty names panicked:
panic: runtime error: index out of range [1] with length 1
diff/diff.go:44
After the fix the function returns ("", true), skipping the entry.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The action panics with
index out of range [1] with length 1atdiff/diff.go:44when a pull request contains a file whose only change is a permission/mode bit (no content diff). The bug is present in both v2.0.0 and v2.1.0.Root cause
checkDiffFilesplitsOrigNameandNewNameon/and unconditionally accesses[1]:For a standard git diff, file names always carry an
a/orb/prefix (or are/dev/null), soSplitNreturns two elements. Mode-only diffs have no---/+++headers at all. Thesourcegraph/go-diffparser therefore returnsOrigName = ""andNewName = ""for those entries:Accessing
[1]on a single-element slice is a runtime panic.How to reproduce
Open a PR containing a file whose only change is the execute bit:
The diff block that triggers it (no
---/+++lines):diff --git a/some-file.yaml b/some-file.yaml old mode 100755 new mode 100644The resulting panic:
Fix
Add a length guard before indexing. Mode-only and header-less diffs have no content to scan, so returning
ignore = trueis the correct behaviour:🤖 This PR was created with the assistance of Claude Code